package cn.zxcoder.controller.pay;

import cn.zxcoder.Tools.ToolUtil;
import cn.zxcoder.entity.Res;
import cn.zxcoder.entity.pay.Cnf;
import cn.zxcoder.entity.pay.CnfInfo;
import cn.zxcoder.entity.pay.Mode;
import cn.zxcoder.service.ICnfInfoService;
import cn.zxcoder.service.ICnfService;
import cn.zxcoder.service.IModeService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zx
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/api/Config")
public class CnfController {

    @Autowired
    ICnfService iCnfServiceIml;
    @Autowired
    IModeService iModeServiceIml;


    @ApiOperation("获取支付配置")
    @PostMapping("/list")
    public Res getCnf(@RequestParam String page, @RequestParam String limit, @RequestParam String cnfName,
                          @RequestParam String mode, @RequestParam String merchantCode) {
        Page<Cnf> ipage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        QueryWrapper<Cnf> wrapper = new QueryWrapper<>();
        wrapper.select(Cnf.class, info -> !info.getColumn().equals("name") && !info.getColumn().equals("subName"));
        if (!cnfName.equals("")) {
            wrapper.like("cnfName",cnfName);
        }
        if (!mode.equals("")) {
            wrapper.eq("mode", Integer.parseInt(mode));
        }
        if (!merchantCode.equals("")) {
            wrapper.like("merchant_code", merchantCode);
        }
//        if (cnfName.equals("")) {//
//            if (merchantCode.equals("")) {
//                if (!mode.equals("")) {
//                    wrapper.eq("mode", Integer.parseInt(mode));
//                }
//            } else {
//                if (mode.equals("")) {
//                    wrapper.like("merchant_code", merchantCode);
//                } else {
//                    wrapper.eq("mode", Integer.parseInt(mode)).like("merchant_code", merchantCode);
//                }
//
//            }
//        } else {
//            if (merchantCode.equals("")) {
//                if (mode.equals("")) {
//                    wrapper.like("cnf_name", cnfName);
//                } else {
//                    wrapper.like("cnf_name", cnfName).eq("mode", Integer.parseInt(mode));
//                }
//            } else {
//                if (mode.equals("")) {
//                    wrapper.like("cnf_name", cnfName).and(i -> {
//                        i.like("merchant_code", merchantCode);
//                    });
//                } else {
//                    wrapper.like("cnf_name", cnfName).eq("mode", Integer.parseInt(mode)).and(i -> {
//                        i.like("merchant_code", merchantCode);
//                    });
//                }
//            }
//        }
        Page<Cnf> page1 = iCnfServiceIml.page(ipage, wrapper);
        if (page1.getRecords().size() > 0) {//有数据再进行下一步
            ToolUtil.setFieldCnf(page1, iModeServiceIml);//进行数据填充
            return new Res(true, JSON.toJSONString(page1), "获取支付配置成功");
        } else {
            return new Res(true, null, "暂时还没有支付配置哦");
        }

    }

    @ApiOperation("保存支付配置")
    @PostMapping("/save")
    public Res saveCnf(@RequestParam boolean issave, @RequestParam String id, @RequestParam String cnfName,
                           @RequestParam String mode, @RequestParam String merchantCode,@RequestParam String subCode,
                           @RequestParam String appId, @RequestParam String callbackInfo,@RequestParam String encryptionMode,
                       @RequestParam String secretKey,@RequestParam String merchantName,@RequestParam String certPath,
                       @RequestParam String certPassword,@RequestParam String rsaKey) {

        if (issave) {//保存支付配置
            Cnf cnf = new Cnf();
            cnf.setCnfName(cnfName);
            cnf.setMode(Integer.parseInt(mode));
            cnf.setMerchantCode(merchantCode);
            cnf.setSubCode(subCode);
            cnf.setAppId(appId);
            cnf.setCallbackInfo(callbackInfo);
            cnf.setEncryptionMode(encryptionMode);
            cnf.setSecretKey(secretKey);
            cnf.setMerchantName(merchantName);
            cnf.setCertPath(certPath);
            cnf.setCertPassword(certPassword);
            cnf.setRsaKey(rsaKey);
            boolean save = iCnfServiceIml.save(cnf);
            return new Res(save, null, save ? "保存配置成功" : "保存配置失败");

        } else {//编辑支付配置
            UpdateWrapper<Cnf> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", id);
            wrapper.set("cnf_name", cnfName);
            wrapper.set("mode", mode);
            wrapper.set("merchant_code", merchantCode);
            wrapper.set("sub_code", subCode);
            wrapper.set("app_id", appId);
            wrapper.set("encryption_mode", encryptionMode);
            wrapper.set("secret_key", secretKey);
            wrapper.set("callback_info", callbackInfo);
            wrapper.set("merchant_name", merchantName);
            wrapper.set("cert_path", certPath);
            wrapper.set("cert_password", certPassword);
            wrapper.set("rsakey", rsaKey);

            boolean update = iCnfServiceIml.update(wrapper);
            return new Res(update, null, update ? "编辑支付配置成功" : "保存支付配置失败");
        }
    }


    @ApiOperation("删除支付配置")
    @DeleteMapping("/delete/{id}")
    public Res deleteCnf(@PathVariable String id) {
        boolean b = iCnfServiceIml.removeById(Integer.parseInt(id));
        return new Res(b, null, b ? "删除支付配置成功" : "删除支付配置失败");
    }




}
